package com.ymatou.iapi.tradingsettlement.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.tradingsettlement.parameter.ManageFeeDetailReq;
import com.ymatou.iapi.tradingsettlement.service.ManageFeeDetailCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.hunter.HunterSettlement;
import com.ymttest.database.sqlwapper.hunter.HunterSettlementWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

@Ignore
public class Ts_ManageFeeDetail {

	private static ManageFeeDetailReq manageFeeDetailReq;
	private static ManageFeeDetailCall manageFeeDetailCall;

	HunterSettlementWrapper hunterSettlementWrapper = new HunterSettlementWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("推手管理服务费明细");
	}

	@Before
	public void caseUp() {
		manageFeeDetailReq = new ManageFeeDetailReq();
		manageFeeDetailCall = new ManageFeeDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(manageFeeDetailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb(JSONArray object) throws JSONException,
			ClientProtocolException, IOException {

		List<HunterSettlement> hunterSettlementList = hunterSettlementWrapper
				.selectByHunterIdAndDate(manageFeeDetailReq.getHunterId(),
						manageFeeDetailReq.getYear(),
						manageFeeDetailReq.getMonth());
		String settlementStatus = "SETTLED";

		for (int i = 0; i < object.length(); i++) {

			HunterSettlement actualObject = hunterSettlementList.get(i);

			if (actualObject.getSettlement_status() != 1)
				settlementStatus = "NOT_SETTLED";
			else if (actualObject.getSettlement_status() == 1)
				settlementStatus = "SETTLED";

			JSONObject expectobject = object.getJSONObject(i);

			DataUtil.verifyEquals(expectobject.getLong("orderId"),
					actualObject.getOrder_id(), "验证orderId");
			DataUtil.verifyDateEquals(expectobject.getString("paidTime"),
					actualObject.getPaid_time(), "验证paidTime");
			DataUtil.verifyEquals(expectobject.getString("settlementStatus"),
					settlementStatus, "验证settlementStatus");
			DataUtil.verifyEquals(
					new BigDecimal(expectobject.optString("totalAmount"))
							.setScale(2), actualObject.getTotal_amount(),
					"验证totalAmount");
		}

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeDetail_001() {
		Logger.start(true, "推手管理服务费明细");
		try {

			manageFeeDetailReq.setHunterId(20359857);
			manageFeeDetailReq.setYear(YMTDateUtil
					.getCurrentYearMonthDate("year"));
			manageFeeDetailReq.setMonth(YMTDateUtil
					.getCurrentYearMonthDate("month"));
			manageFeeDetailReq.setPageIndex(1);
			manageFeeDetailReq.setPageSize(10);

			manageFeeDetailCall.setData(manageFeeDetailReq);
			manageFeeDetailCall.callService();

			verifyDb(manageFeeDetailCall.getResult());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeDetail_002() {
		Logger.start(true, "推手管理服务费明细（没有管理费，只产生了佣金）");
		try {

			manageFeeDetailReq.setHunterId(1022L);
			manageFeeDetailReq.setYear(YMTDateUtil
					.getCurrentYearMonthDate("year"));
			manageFeeDetailReq.setMonth(YMTDateUtil
					.getCurrentYearMonthDate("month"));
			manageFeeDetailReq.setPageIndex(1);
			manageFeeDetailReq.setPageSize(10);

			manageFeeDetailCall.setData(manageFeeDetailReq);
			manageFeeDetailCall.callService();

			DataUtil.verifyEquals(null, manageFeeDetailCall.getResponseData()
					.getJSONObject("data").get("results"), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeDetail_003() {
		Logger.start(true, "年份小于2018");
		try {

			manageFeeDetailReq.setHunterId(74971288000L);
			manageFeeDetailReq.setYear(2017);
			manageFeeDetailReq.setMonth(YMTDateUtil
					.getCurrentYearMonthDate("month"));
			manageFeeDetailReq.setPageIndex(1);
			manageFeeDetailReq.setPageSize(10);

			manageFeeDetailCall.setData(manageFeeDetailReq);
			manageFeeDetailCall.callService();

			DataUtil.verifyEquals(400, manageFeeDetailCall.getResponseData()
					.getInt("code"), "验证code");
			DataUtil.verifyEquals(100, manageFeeDetailCall.getResponseData()
					.getInt("bcode"), "验证bcode");
			DataUtil.verifyEquals("year:年份不能小于2018年", manageFeeDetailCall
					.getResponseData().getString("msg"), "验证msg");
			DataUtil.verifyEquals(false, manageFeeDetailCall.getResponseData()
					.getJSONObject("data").getBoolean("success"), "验证success");
			DataUtil.verifyEquals("ILLEGAL_ARGUMENT",
					manageFeeDetailCall.getResponseData().getJSONObject("data")
							.getString("errorCode"), "验证errorCode");
			DataUtil.verifyEquals("year:年份不能小于2018年",
					manageFeeDetailCall.getResponseData().getJSONObject("data")
							.getString("errorMessage"), "验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeDetail_004() {
		Logger.start(true, "月份必须在1至12月之前");
		try {

			manageFeeDetailReq.setHunterId(74971288000L);
			manageFeeDetailReq.setYear(2018);
			manageFeeDetailReq.setMonth(13);
			manageFeeDetailReq.setPageIndex(1);
			manageFeeDetailReq.setPageSize(10);

			manageFeeDetailCall.setData(manageFeeDetailReq);
			manageFeeDetailCall.callService();

			DataUtil.verifyEquals(400, manageFeeDetailCall.getResponseData()
					.getInt("code"), "验证code");
			DataUtil.verifyEquals(100, manageFeeDetailCall.getResponseData()
					.getInt("bcode"), "验证bcode");
			DataUtil.verifyEquals("month:月份必须在1至12月之前", manageFeeDetailCall
					.getResponseData().getString("msg"), "验证msg");
			DataUtil.verifyEquals(false, manageFeeDetailCall.getResponseData()
					.getJSONObject("data").getBoolean("success"), "验证success");
			DataUtil.verifyEquals("ILLEGAL_ARGUMENT",
					manageFeeDetailCall.getResponseData().getJSONObject("data")
							.getString("errorCode"), "验证errorCode");
			DataUtil.verifyEquals("month:月份必须在1至12月之前",
					manageFeeDetailCall.getResponseData().getJSONObject("data")
							.getString("errorMessage"), "验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeDetail_005() {
		Logger.start(true, "推手id为空");
		try {

			manageFeeDetailReq.setHunterId(0);
			manageFeeDetailReq.setYear(2018);
			manageFeeDetailReq.setMonth(12);
			manageFeeDetailReq.setPageIndex(1);
			manageFeeDetailReq.setPageSize(10);

			manageFeeDetailCall.setData(manageFeeDetailReq);
			manageFeeDetailCall.callService();

			DataUtil.verifyEquals(400, manageFeeDetailCall.getResponseData()
					.getInt("code"), "验证code");
			DataUtil.verifyEquals(100, manageFeeDetailCall.getResponseData()
					.getInt("bcode"), "验证bcode");
			DataUtil.verifyEquals("hunterId:推手id不能为空", manageFeeDetailCall
					.getResponseData().getString("msg"), "验证msg");
			DataUtil.verifyEquals(false, manageFeeDetailCall.getResponseData()
					.getJSONObject("data").getBoolean("success"), "验证success");
			DataUtil.verifyEquals("ILLEGAL_ARGUMENT",
					manageFeeDetailCall.getResponseData().getJSONObject("data")
							.getString("errorCode"), "验证errorCode");
			DataUtil.verifyEquals("hunterId:推手id不能为空",
					manageFeeDetailCall.getResponseData().getJSONObject("data")
							.getString("errorMessage"), "验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeDetail_006() {
		Logger.start(true, "推手在该月没有数据");
		try {

			manageFeeDetailReq.setHunterId(1);
			manageFeeDetailReq.setYear(2018);
			manageFeeDetailReq.setMonth(12);
			manageFeeDetailReq.setPageIndex(1);
			manageFeeDetailReq.setPageSize(10);

			manageFeeDetailCall.setData(manageFeeDetailReq);
			manageFeeDetailCall.callService();

			DataUtil.verifyEquals(200, manageFeeDetailCall.getResponseData()
					.getInt("code"), "验证code");
			DataUtil.verifyEquals(0, manageFeeDetailCall.getResponseData()
					.getInt("bcode"), "验证bcode");
			DataUtil.verifyEquals(true, manageFeeDetailCall.getResponseData()
					.getJSONObject("data").getBoolean("success"), "验证success");
			DataUtil.verifyEquals(null, manageFeeDetailCall.getResult(),
					"验证results");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ManageFeeDetail_007() {
		Logger.start(true, "pageIndex为空");
		try {

			manageFeeDetailReq.setHunterId(11047305000L);
			manageFeeDetailReq.setYear(2018);
			manageFeeDetailReq.setMonth(12);
			manageFeeDetailReq.setPageIndex(0);
			manageFeeDetailReq.setPageSize(10);

			manageFeeDetailCall.setData(manageFeeDetailReq);
			manageFeeDetailCall.callService();

			DataUtil.verifyEquals(500, manageFeeDetailCall.getResponseData()
					.getInt("code"), "验证code");
			DataUtil.verifyEquals(199, manageFeeDetailCall.getResponseData()
					.getInt("bcode"), "验证bcode");
			DataUtil.verifyEquals("系统繁忙，请稍后重试", manageFeeDetailCall
					.getResponseData().getString("msg"), "验证msg");
			DataUtil.verifyEquals(false, manageFeeDetailCall.getResponseData()
					.getJSONObject("data").getBoolean("success"), "验证success");
			DataUtil.verifyEquals("UNKNOWN",
					manageFeeDetailCall.getResponseData().getJSONObject("data")
							.getString("errorCode"), "验证errorCode");
			DataUtil.verifyEquals("系统繁忙，请稍后重试",
					manageFeeDetailCall.getResponseData().getJSONObject("data")
							.getString("errorMessage"), "验证errorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
